#setwd("C:/Users/uctprke/Dropbox/Research/Risk Paper/SH Experiment/Data/Analysis")
setwd("C:/Users/Kendall/Dropbox/Research/Risk Paper/SH Experiment/Data/Analysis")


library(ggplot2)
mydata<- read.csv("Game1Data.csv")
attach(mydata)
labels(mydata)
obs<-length(mydata$Subject)
NumSess<-4
library(ggplot2)
Choice<-matrix(nrow=75,ncol=16)

#Loading Data for Game 1
PropSG1<-numeric(75)
for(i in 1:75){PropSG1[i]<-(sum(mydata$Period == i & mydata$Choice==1))/(NumSess*16)}

PropSG1Sess2G1<-numeric(75)
for(i in 1:75){PropSG1Sess2G1[i]<-(sum(mydata$Period == i & mydata$Choice==1 & mydata$Session==2 & mydata$Group==1))/(8)}

PropSG1Sess2G2<-numeric(75)
for(i in 1:75){PropSG1Sess2G2[i]<-(sum(mydata$Period == i & mydata$Choice==1 & mydata$Session==2 & mydata$Group==2))/(8)}

PropSG1Sess3G1<-numeric(75)
for(i in 1:75){PropSG1Sess3G1[i]<-(sum(mydata$Period == i & mydata$Choice==1 & mydata$Session==3 & mydata$Group==1))/(8)}

PropSG1Sess3G2<-numeric(75)
for(i in 1:75){PropSG1Sess3G2[i]<-(sum(mydata$Period == i & mydata$Choice==1 & mydata$Session==3 & mydata$Group==2))/(8)}

PropSG1Sess6G1<-numeric(75)
for(i in 1:75){PropSG1Sess6G1[i]<-(sum(mydata$Period == i & mydata$Choice==1 & mydata$Session==6 & mydata$Group==1))/(8)}

PropSG1Sess6G2<-numeric(75)
for(i in 1:75){PropSG1Sess6G2[i]<-(sum(mydata$Period == i & mydata$Choice==1 & mydata$Session==6 & mydata$Group==2))/(8)}

PropSG1Sess7G1<-numeric(75)
for(i in 1:75){PropSG1Sess7G1[i]<-(sum(mydata$Period == i & mydata$Choice==1 & mydata$Session==7 & mydata$Group==1))/(8)}

PropSG1Sess7G2<-numeric(75)
for(i in 1:75){PropSG1Sess7G2[i]<-(sum(mydata$Period == i & mydata$Choice==1 & mydata$Session==7 & mydata$Group==2))/(8)}


plot(PropSG1)


#Loading Data for Game 2
mydata<- read.csv("Game2Data.csv")
attach(mydata)
labels(mydata)
obs<-length(mydata$Subject)
library(ggplot2)
Choice<-matrix(nrow=75,ncol=16)


NumSess<-4
PropSG2<-numeric(75)
for(i in 1:75){PropSG2[i]<-(sum(mydata$Period == i & mydata$Choice==1))/(NumSess*16)}



PropSG2Sess1G1<-numeric(75)
for(i in 1:75){PropSG2Sess1G1[i]<-(sum(mydata$Period == i & mydata$Choice==1 & mydata$Session==1 & mydata$Group==1))/(8)}

PropSG2Sess1G2<-numeric(75)
for(i in 1:75){PropSG2Sess1G2[i]<-(sum(mydata$Period == i & mydata$Choice==1 & mydata$Session==1 & mydata$Group==2))/(8)}

PropSG2Sess4G1<-numeric(75)
for(i in 1:75){PropSG2Sess4G1[i]<-(sum(mydata$Period == i & mydata$Choice==1 & mydata$Session==4 & mydata$Group==1))/(8)}

PropSG2Sess4G2<-numeric(75)
for(i in 1:75){PropSG2Sess4G2[i]<-(sum(mydata$Period == i & mydata$Choice==1 & mydata$Session==4 & mydata$Group==2))/(8)}

PropSG2Sess5G1<-numeric(75)
for(i in 1:75){PropSG2Sess5G1[i]<-(sum(mydata$Period == i & mydata$Choice==1 & mydata$Session==5 & mydata$Group==1))/(8)}

PropSG2Sess5G2<-numeric(75)
for(i in 1:75){PropSG2Sess5G2[i]<-(sum(mydata$Period == i & mydata$Choice==1 & mydata$Session==5 & mydata$Group==2))/(8)}

PropSG2Sess8G1<-numeric(75)
for(i in 1:75){PropSG2Sess8G1[i]<-(sum(mydata$Period == i & mydata$Choice==1 & mydata$Session==8 & mydata$Group==1))/(8)}

PropSG2Sess8G2<-numeric(75)
for(i in 1:75){PropSG2Sess8G2[i]<-(sum(mydata$Period == i & mydata$Choice==1 & mydata$Session==8 & mydata$Group==2))/(8)}

plot(PropSG2)

#FIGURE 5

my_grob1 = grobTree(textGrob("Game 1", x=0.01,  y=0.58, hjust=0,
                             gp=gpar(col="black", fontsize=15, fontface="italic")))
my_grob2 = grobTree(textGrob("Game 2", x=0.01,  y=0.89, hjust=0,
                             gp=gpar(col="black", fontsize=15, fontface="italic")))
#Plot by game
df <- data.frame(x = c(1:75), y = PropSG1, z = PropSG2)
PlotMain<-ggplot(df, aes(x)) + 
  geom_line(aes(y = PropSG1), colour = "grey", linetype = "solid", size=1) + 
  geom_line(aes(y = PropSG2), colour = "black", linetype = "twodash", size=1) + theme_classic() +xlab("Period")+ylab(NULL)+
  theme(legend.title=element_blank())+ ylim(0, 1)+ scale_x_continuous(breaks=c(1,25, 50, 75))

print(PlotMain + ggtitle("Proportion in Action X")+ theme(plot.title = element_text(family="serif", size=20,hjust = 0.5))+
        annotation_custom(my_grob1)+
        annotation_custom(my_grob2))


#testing final 3 periods
i=73
G1Peri<-c(PropSG1Sess2G1[i], PropSG1Sess2G2[i], PropSG1Sess3G1[i],PropSG1Sess3G2[i], PropSG1Sess6G1[i],PropSG1Sess6G2[i],PropSG1Sess7G1[i],PropSG1Sess7G2[i], PropSG1Sess2G1[i+1], PropSG1Sess2G2[i+1], PropSG1Sess3G1[i+1],PropSG1Sess3G2[i+1], PropSG1Sess6G1[i+1],PropSG1Sess6G2[i+1],PropSG1Sess7G1[i+1],PropSG1Sess7G2[i+1],     PropSG1Sess2G1[i+2], PropSG1Sess2G2[i+2], PropSG1Sess3G1[i+2],PropSG1Sess3G2[i+2], PropSG1Sess6G1[i+2],PropSG1Sess6G2[i+2],PropSG1Sess7G1[i+2],PropSG1Sess7G2[i+2])
G2Peri<-c(PropSG2Sess1G1[i], PropSG2Sess1G2[i], PropSG2Sess4G1[i],PropSG2Sess4G2[i], PropSG2Sess5G1[i],PropSG2Sess5G2[i],PropSG2Sess8G1[i],PropSG2Sess8G2[i], PropSG2Sess1G1[i+1], PropSG2Sess1G2[i+1], PropSG2Sess4G1[i+1],PropSG2Sess4G2[i+1], PropSG2Sess5G1[i+1],PropSG2Sess5G2[i+1],PropSG2Sess8G1[i+1],PropSG2Sess8G2[i+1],     PropSG2Sess1G1[i+2], PropSG2Sess1G2[i+2], PropSG2Sess4G1[i+2],PropSG2Sess4G2[i+2], PropSG2Sess5G1[i+2],PropSG2Sess5G2[i+2],PropSG2Sess8G1[i+2],PropSG2Sess8G2[i+2])
t.test(G1Peri,G2Peri)
wilcox.test(G1Peri,G2Peri)


#grouped at single periods
TTests<-numeric(75)
WTests<-numeric(75)
for(i in 1:75){
  G1Peri<-c(PropSG1Sess2G1[i], PropSG1Sess2G2[i], PropSG1Sess3G1[i],PropSG1Sess3G2[i], PropSG1Sess6G1[i],PropSG1Sess6G2[i],PropSG1Sess7G1[i],PropSG1Sess7G2[i])
  G2Peri<-c(PropSG2Sess1G1[i], PropSG2Sess1G2[i], PropSG2Sess4G1[i],PropSG2Sess4G2[i], PropSG2Sess5G1[i],PropSG2Sess5G2[i],PropSG2Sess8G1[i],PropSG2Sess8G2[i])
  x<-t.test(G1Peri,G2Peri)
  y<-wilcox.test(G1Peri,G2Peri)
  TTests[i]<-x$p.value
  WTests[i]<-y$p.value
}

#Figure 6
plot(TTests)
P1t<-plot(TTests, ylim=c(0, .2))

#Figure 16 (APPENDIX C)
plot(WTests)
P1w<-plot(WTests, ylim=c(0, .6))



#Earnings Test
G1Earn<-numeric(64)
G2Earn<-numeric(64)

G1Earn<-c(4608,4519,4552,	4499,	4525,	4535,	4210,	4580,	4541,	4565,	4532,	4522,	4510,	4505,	4565,	4516,	4639,	4650,	4650,	4525,	4463,	4461	,4650,	4636	,4636	,4376	,4485	,4509	,4639	,4650	,4492,	4451	,4480	,4505	,4335	,4516	,4550	,4565,	4354	,4495	,4496	,4533,	4555,	4555,	4473,	4525,	4511,	4543,	4650,	4628,	4650,	4636,	4650,	4650,	4636,	4650,	4636,	4650,	4650,	4650,	4650,	4650,	4636,	4628)
G2Earn<-c(4570,	4357,	3831,	7275,	7264,	7275,	3646,	4410,	4838,	7275,	7275,	7275,	4085,	4031,	7275,	7191,	7191,	7191,	7191,	7096,	7107,	7191,	7231,	7264,	7275,	7191,	7180,	7187,	7107,	7275,	7023,	7275,	7023,	6771,	6928,	6519,	6751,	6833,	7107,	6939,	6917,	6703,	6665,	6771,	6855,	6905,	6771,	6486,	7107,	7275,	7275,	7096,	6855,	6928,	7158,	7191,	7275,	7074,	7107,	7107,	7275,	7264,	6939,	7004)
t.test(G1Earn,G2Earn)
wilcox.test(G1Earn,G2Earn)

G1EarnAvg<-numeric(8)
G2EarnAvg<-numeric(8)
G1EarnAvg<-c(4503.5, 4532, 4584.25, 4529.75, 4475,4523.875, 4643.75, 4643.75)
G2EarnAvg<-c(5328.5,6155.625,7182.75,7189.125,6858.875,	6759.125,	7110.625,	7130.625)
t.test(G1EarnAvg,G2EarnAvg)
wilcox.test(G1EarnAvg,G2EarnAvg)




#FIGURE 15 (APPENDIX C)
my_grob1 = grobTree(textGrob("(a)", x=.50,  y=0.8, hjust=0,
                             gp=gpar(col="black", fontsize=15, fontface="italic")))
my_grob2 = grobTree(textGrob("(b)", x=.50,  y=0.8, hjust=0,
                             gp=gpar(col="black", fontsize=15, fontface="italic")))
my_grob3 = grobTree(textGrob("(c)", x=.50,  y=0.8, hjust=0,
                             gp=gpar(col="black", fontsize=15, fontface="italic")))
my_grob4 = grobTree(textGrob("(d)", x=.50,  y=0.8, hjust=0,
                             gp=gpar(col="black", fontsize=15, fontface="italic")))
my_grob5 = grobTree(textGrob("(e)", x=.50,  y=0.8, hjust=0,
                             gp=gpar(col="black", fontsize=15, fontface="italic")))
my_grob6 = grobTree(textGrob("(f)", x=.50,  y=0.8, hjust=0,
                             gp=gpar(col="black", fontsize=15, fontface="italic")))
my_grob7 = grobTree(textGrob("(g)", x=.50,  y=0.8, hjust=0,
                             gp=gpar(col="black", fontsize=15, fontface="italic")))
my_grob8 = grobTree(textGrob("(h)", x=.50,  y=0.8, hjust=0,
                             gp=gpar(col="black", fontsize=15, fontface="italic")))

#Plot sessions individually Game 1
plot1<-ggplot(df, aes(x)) + geom_line(aes(y=PropSG1Sess2G1), colour = "grey", linetype="solid", size=1) + theme_classic() +xlab("Period")+ylab(NULL)+
  theme(legend.title=element_blank())+ ylim(0, 1)+ scale_x_continuous(breaks=c(1,25, 50, 75))+ annotation_custom(my_grob4)
plot2<-ggplot(df, aes(x)) + geom_line(aes(y=PropSG1Sess2G2), colour = "grey", linetype="solid", size=1) + theme_classic() +xlab("Period")+ylab(NULL)+
  theme(legend.title=element_blank())+ ylim(0, 1)+ scale_x_continuous(breaks=c(1,25, 50, 75))+ annotation_custom(my_grob7)
plot3<-ggplot(df, aes(x)) + geom_line(aes(y=PropSG1Sess3G1), colour = "grey", linetype="solid", size=1) + theme_classic() +xlab("Period")+ylab(NULL)+
  theme(legend.title=element_blank())+ ylim(0, 1)+ scale_x_continuous(breaks=c(1,25, 50, 75))+ annotation_custom(my_grob3)
plot4<-ggplot(df, aes(x)) + geom_line(aes(y=PropSG1Sess3G2), colour = "grey", linetype="solid", size=1) + theme_classic() +xlab("Period")+ylab(NULL)+
  theme(legend.title=element_blank())+ ylim(0, 1)+ scale_x_continuous(breaks=c(1,25, 50, 75))+ annotation_custom(my_grob5)
plot5<-ggplot(df, aes(x)) + geom_line(aes(y=PropSG1Sess6G1), colour = "grey", linetype="solid", size=1) + theme_classic() +xlab("Period")+ylab(NULL)+
  theme(legend.title=element_blank())+ ylim(0, 1)+ scale_x_continuous(breaks=c(1,25, 50, 75))+ annotation_custom(my_grob6)
plot6<-ggplot(df, aes(x)) + geom_line(aes(y=PropSG1Sess6G2), colour = "grey", linetype="solid", size=1) + theme_classic() +xlab("Period")+ylab(NULL)+
  theme(legend.title=element_blank())+ ylim(0, 1)+ scale_x_continuous(breaks=c(1,25, 50, 75))+ annotation_custom(my_grob8)
plot7<-ggplot(df, aes(x)) + geom_line(aes(y=PropSG1Sess7G1), colour = "grey", linetype="solid", size=1) + theme_classic() +xlab("Period")+ylab(NULL)+
  theme(legend.title=element_blank())+ ylim(0, 1)+ scale_x_continuous(breaks=c(1,25, 50, 75))+ annotation_custom(my_grob2)
plot8<-ggplot(df, aes(x)) + geom_line(aes(y=PropSG1Sess7G2), colour = "grey", linetype="solid", size=1) + theme_classic() +xlab("Period")+ylab(NULL)+
  theme(legend.title=element_blank())+ ylim(0, 1)+ scale_x_continuous(breaks=c(1,25, 50, 75))+ annotation_custom(my_grob1)

# FIGURE 15 TOP PANEL (APPENDIX C)
multiplot(plot8, plot4, plot7, plot5, plot3, plot2, plot1, plot6, cols=4, title = "Game 1 - Proportion in Action X", 
          fontsize = 20, fontfamily = "serif")



my_grob1 = grobTree(textGrob("(a)", x=.50,  y=0.8, hjust=0,
                             gp=gpar(col="black", fontsize=15, fontface="italic")))
my_grob2 = grobTree(textGrob("(b)", x=.50,  y=0.8, hjust=0,
                             gp=gpar(col="black", fontsize=15, fontface="italic")))
my_grob3 = grobTree(textGrob("(c)", x=.50,  y=0.8, hjust=0,
                             gp=gpar(col="black", fontsize=15, fontface="italic")))
my_grob4 = grobTree(textGrob("(d)", x=.50,  y=0.8, hjust=0,
                             gp=gpar(col="black", fontsize=15, fontface="italic")))
my_grob5 = grobTree(textGrob("(e)", x=.50,  y=0.8, hjust=0,
                             gp=gpar(col="black", fontsize=15, fontface="italic")))
my_grob6 = grobTree(textGrob("(f)", x=.50,  y=0.8, hjust=0,
                             gp=gpar(col="black", fontsize=15, fontface="italic")))
my_grob7 = grobTree(textGrob("(g)", x=.50,  y=0.8, hjust=0,
                             gp=gpar(col="black", fontsize=15, fontface="italic")))
my_grob8 = grobTree(textGrob("(h)", x=.50,  y=0.8, hjust=0,
                             gp=gpar(col="black", fontsize=15, fontface="italic")))

#Plot sessions individually Game 2
plot1<-ggplot(df, aes(x)) + geom_line(aes(y=PropSG2Sess1G1), colour = "black", linetype="twodash", size=1) + theme_classic() +xlab("Period")+ylab(NULL)+
  theme(legend.title=element_blank())+ ylim(0, 1)+ scale_x_continuous(breaks=c(1,25, 50, 75))+ annotation_custom(my_grob8)
plot2<-ggplot(df, aes(x)) + geom_line(aes(y=PropSG2Sess1G2), colour = "black", linetype="twodash", size=1) + theme_classic() +xlab("Period")+ylab(NULL)+
  theme(legend.title=element_blank())+ ylim(0, 1)+ scale_x_continuous(breaks=c(1,25, 50, 75))+ annotation_custom(my_grob1)
plot3<-ggplot(df, aes(x)) + geom_line(aes(y=PropSG2Sess4G1), colour = "black", linetype="twodash", size=1) + theme_classic() +xlab("Period")+ylab(NULL)+
  theme(legend.title=element_blank())+ ylim(0, 1)+ scale_x_continuous(breaks=c(1,25, 50, 75))+ annotation_custom(my_grob3)
plot4<-ggplot(df, aes(x)) + geom_line(aes(y=PropSG2Sess4G2), colour = "black", linetype="twodash", size=1) + theme_classic() +xlab("Period")+ylab(NULL)+
  theme(legend.title=element_blank())+ ylim(0, 1)+ scale_x_continuous(breaks=c(1,25, 50, 75))+ annotation_custom(my_grob4)
plot5<-ggplot(df, aes(x)) + geom_line(aes(y=PropSG2Sess5G1), colour = "black", linetype="twodash", size=1) + theme_classic() +xlab("Period")+ylab(NULL)+
  theme(legend.title=element_blank())+ ylim(0, 1)+ scale_x_continuous(breaks=c(1,25, 50, 75))+ annotation_custom(my_grob5)
plot6<-ggplot(df, aes(x)) + geom_line(aes(y=PropSG2Sess5G2), colour = "black", linetype="twodash", size=1) + theme_classic() +xlab("Period")+ylab(NULL)+
  theme(legend.title=element_blank())+ ylim(0, 1)+ scale_x_continuous(breaks=c(1,25, 50, 75))+ annotation_custom(my_grob7)
plot7<-ggplot(df, aes(x)) + geom_line(aes(y=PropSG2Sess8G1), colour = "black", linetype="twodash", size=1) + theme_classic() +xlab("Period")+ylab(NULL)+
  theme(legend.title=element_blank())+ ylim(0, 1)+ scale_x_continuous(breaks=c(1,25, 50, 75))+ annotation_custom(my_grob2)
plot8<-ggplot(df, aes(x)) + geom_line(aes(y=PropSG2Sess8G2), colour = "black", linetype="twodash", size=1) + theme_classic() +xlab("Period")+ylab(NULL)+
  theme(legend.title=element_blank())+ ylim(0, 1)+ scale_x_continuous(breaks=c(1,25, 50, 75))+ annotation_custom(my_grob6)


# FIGURE 15 BOTTOM PANEL (APPENDIX C)
multiplot(plot2, plot5, plot7, plot8, plot3, plot6, plot4, plot1, cols=4, title = "Game 2 - Proportion in Action X", 
          fontsize = 20, fontfamily = "serif")











